#include "TCPServerConnectionWrapper.h"

network::TCPServerConnectionWrapper::TCPServerConnectionWrapper(std::vector<TCPServerChannel::Ptr>& vecChannels, StreamSocket& sock)
: TCPServerConnection(sock)
, _vecChannels(vecChannels)
, _rw(sock)
, _io(_rw)
{
}

network::TCPServerConnectionWrapper::~TCPServerConnectionWrapper()
{
}

void network::TCPServerConnectionWrapper::run()
{
//	TCPServerPacketReaderWriter rw(socket());
//	TCPServerPacketReceiverWrapper<TCPServerPacket, TCPServerPacketReaderWriter> io(rw);

	try
	{
		while (true)
		{
			//poco_information(NetworkSubsystem::instance().logger(), "wait to receive packet...");
			TCPServerPacket::Ptr packet = _io.recv();

			if (!packet.isNull())
			{
				//poco_information(NetworkSubsystem::instance().logger(), std::string("received packet: ") + p->getDataString());
				//poco_information(NetworkSubsystem::instance().logger(), std::string("packet type: ") + std::to_string(p->getHeader()->type));
				TCPServerDispatcherPacket::Ptr dispatchPacket = new TCPServerDispatcherPacket(packet);

				TPacketParams params(dispatchPacket, _rw);
				CustomNotification<TPacketParams> *pNotif = new CustomNotification<TPacketParams>(params);
				//_nq.enqueueNotification(pNotif);
				//_dispatcher.enquenceNotification(packet->getChannelID(), pNotif);
				_vecChannels[packet->getChannelID()]->enquenceNotification(pNotif);
			}
		}
	}
	catch (Poco::Exception& exc)
	{
		//poco_information(NetworkSubsystem::instance().logger(), "AppServConnection: " + exc.displayText());
	}
}
